package com.calculation;
import java.util.ArrayList;
import java.util.List;

public class Exercise {
    private List<Equation> equations = new ArrayList<>();

    public void generateExercises(int count) {
        equations.clear();
        for (int i = 0; i < count; i++) {
            Equation eq = new Equation();
            eq.generateRandom();
            if (!contains(eq)) {
                equations.add(eq);
            } else {
                i--;
            }
        }
    }

    private boolean contains(Equation newEq) {
        for (Equation eq : equations) {
            if (eq.getOperand1() == newEq.getOperand1() &&
                    eq.getOperand2() == newEq.getOperand2() &&
                    eq.getOperator() == newEq.getOperator()) {
                return true;
            }
        }
        return false;
    }

    public void displayExercises() {
        System.out.println("加减法口算练习题");
        System.out.println("=================");

        int columns = 4;
        for (int i = 0; i < equations.size(); i++) {
            System.out.printf("%2d. %-12s", i + 1, equations.get(i).toString());

            if ((i + 1) % columns == 0) {
                System.out.println();
            } else if (i == equations.size() - 1) {
                System.out.println();
            } else {
                System.out.print("  ");
            }
        }
    }
}